home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / cpp / cexp.y < prev    next >
Text File  |  1995-06-15  |  24KB  |  1,048 lines

  1. /* Parse C expressions for CCCP.
  2.    Copyright (C) 1987, 1992, 1994, 1995 Free Software Foundation.
  3.  
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA.
  18.  
  19.  In other words, you are welcome to use, share and improve this program.
  20.  You are forbidden to forbid anyone else to use, share and improve
  21.  what you give them.   Help stamp out software-hoarding!
  22.  
  23.  Adapted from expread.y of GDB by Paul Rubin, July 1986.  */
  24.  
  25. /* Parse a C expression from text in a string  */
  26.    
  27. %{
  28. #include "config.h"
  29. #include <setjmp.h>
  30. /* #define YYDEBUG 1 */
  31.  
  32. #ifdef MULTIBYTE_CHARS
  33. #include <stdlib.h>
  34. #include <locale.h>
  35. #endif
  36.  
  37. #include <stdio.h>
  38.  
  39. typedef unsigned char U_CHAR;
  40.  
  41. /* This is used for communicating lists of keywords with cccp.c.  */
  42. struct arglist {
  43.   struct arglist *next;
  44.   U_CHAR *name;
  45.   int length;
  46.   int argno;
  47. };
  48.  
  49. /* Define a generic NULL if one hasn't already been defined.  */
  50.  
  51. #ifndef NULL
  52. #define NULL 0
  53. #endif
  54.  
  55. #ifndef GENERIC_PTR
  56. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  57. #define GENERIC_PTR void *
  58. #else
  59. #define GENERIC_PTR char *
  60. #endif
  61. #endif
  62.  
  63. /* Find the largest host integer type and set its size and type.  */
  64.  
  65. #ifndef HOST_BITS_PER_WIDE_INT
  66.  
  67. #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
  68. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
  69. #define HOST_WIDE_INT long
  70. #else
  71. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
  72. #define HOST_WIDE_INT int
  73. #endif
  74.  
  75. #endif
  76.  
  77. #ifndef NULL_PTR
  78. #define NULL_PTR ((GENERIC_PTR)0)
  79. #endif
  80.  
  81. int yylex ();
  82. void yyerror ();
  83. HOST_WIDE_INT expression_value;
  84.  
  85. static jmp_buf parse_return_error;
  86.  
  87. /* Nonzero means count most punctuation as part of a name.  */
  88. static int keyword_parsing = 0;
  89.  
  90. /* Nonzero means do not evaluate this expression.
  91.    This is a count, since unevaluated expressions can nest.  */
  92. static int skip_evaluation;
  93.  
  94. /* some external tables of character types */
  95. extern unsigned char is_idstart[], is_idchar[], is_hor_space[];
  96.  
  97. extern char *xmalloc ();
  98.  
  99. /* Flag for -pedantic.  */
  100. extern int pedantic;
  101.  
  102. /* Flag for -traditional.  */
  103. extern int traditional;
  104.  
  105. #ifndef CHAR_TYPE_SIZE
  106. #define CHAR_TYPE_SIZE BITS_PER_UNIT
  107. #endif
  108.  
  109. #ifndef INT_TYPE_SIZE
  110. #define INT_TYPE_SIZE BITS_PER_WORD
  111. #endif
  112.  
  113. #ifndef LONG_TYPE_SIZE
  114. #define LONG_TYPE_SIZE BITS_PER_WORD
  115. #endif
  116.  
  117. #ifndef WCHAR_TYPE_SIZE
  118. #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
  119. #endif
  120.  
  121. #ifndef MAX_CHAR_TYPE_SIZE
  122. #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
  123. #endif
  124.  
  125. #ifndef MAX_INT_TYPE_SIZE
  126. #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
  127. #endif
  128.  
  129. #ifndef MAX_LONG_TYPE_SIZE
  130. #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
  131. #endif
  132.  
  133. #ifndef MAX_WCHAR_TYPE_SIZE
  134. #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
  135. #endif
  136.  
  137. /* Yield nonzero if adding two numbers with A's and B's signs can yield a
  138.    number with SUM's sign, where A, B, and SUM are all C integers.  */
  139. #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
  140.  
  141. static void integer_overflow ();
  142. static long left_shift ();
  143. static long right_shift ();
  144. %}
  145.  
  146. %union {
  147.   struct constant {long value; int unsignedp;} integer;
  148.   struct name {U_CHAR *address; int length;} name;
  149.   struct arglist *keywords;
  150. }
  151.  
  152. %type <integer> exp exp1 start
  153. %type <keywords> keywords
  154. %token <integer> INT CHAR
  155. %token <name> NAME
  156. %token <integer> ERROR
  157.  
  158. %right '?' ':'
  159. %left ','
  160. %left OR
  161. %left AND
  162. %left '|'
  163. %left '^'
  164. %left '&'
  165. %left EQUAL NOTEQUAL
  166. %left '<' '>' LEQ GEQ
  167. %left LSH RSH
  168. %left '+' '-'
  169. %left '*' '/' '%'
  170. %right UNARY
  171.  
  172. /* %expect 40 */
  173.  
  174. %%
  175.  
  176. start   :    exp1
  177.         { expression_value = $1.value; }
  178.     ;
  179.  
  180. /* Expressions, including the comma operator.  */
  181. exp1    :    exp
  182.     |    exp1 ',' exp
  183.             { if (pedantic)
  184.                 pedwarn ("comma operator in operand of `#if'");
  185.               $$ = $3; }
  186.     ;
  187.  
  188. /* Expressions, not including the comma operator.  */
  189. exp    :    '-' exp    %prec UNARY
  190.             { $$.value = - $2.value;
  191.               if (($$.value & $2.value) < 0 && ! $2.unsignedp)
  192.                 integer_overflow ();
  193.               $$.unsignedp = $2.unsignedp; }
  194.     |    '!' exp    %prec UNARY
  195.             { $$.value = ! $2.value;
  196.               $$.unsignedp = 0; }
  197.     |    '+' exp    %prec UNARY
  198.             { $$ = $2; }
  199.     |    '~' exp    %prec UNARY
  200.             { $$.value = ~ $2.value;
  201.               $$.unsignedp = $2.unsignedp; }
  202.     |    '#' NAME
  203.               { $$.value = check_assertion ($2.address, $2.length,
  204.                               0, NULL_PTR);
  205.               $$.unsignedp = 0; }
  206.     |    '#' NAME
  207.             { keyword_parsing = 1; }
  208.         '(' keywords ')'
  209.               { $$.value = check_assertion ($2.address, $2.length,
  210.                               1, $5);
  211.               keyword_parsing = 0;
  212.               $$.unsignedp = 0; }
  213.     |    '(' exp1 ')'
  214.             { $$ = $2; }
  215.     ;
  216.  
  217. /* Binary operators in order of decreasing precedence.  */
  218. exp    :    exp '*' exp
  219.             { $$.unsignedp = $1.unsignedp || $3.unsignedp;
  220.               if ($$.unsignedp)
  221.                 $$.value = (unsigned long) $1.value * $3.value;
  222.               else
  223.                 {
  224.                   $$.value = $1.value * $3.value;
  225.                   if ($1.value
  226.                   && ($$.value / $1.value != $3.value
  227.                       || ($$.value & $1.value & $3.value) < 0))
  228.                 integer_overflow ();
  229.                 } }
  230.     |    exp '/' exp
  231.             { if ($3.value == 0)
  232.                 {
  233.                   if (!skip_evaluation)
  234.                 error ("division by zero in #if");
  235.                   $3.value = 1;
  236.                 }
  237.               $$.unsignedp = $1.unsignedp || $3.unsignedp;
  238.               if ($$.unsignedp)
  239.                 $$.value = (unsigned long) $1.value / $3.value;
  240.               else
  241.                 {
  242.                   $$.value = $1.value / $3.value;
  243.                   if (($$.value & $1.value & $3.value) < 0)
  244.                 integer_overflow ();
  245.                 } }
  246.     |    exp '%' exp
  247.             { if ($3.value == 0)
  248.                 {
  249.                   if (!skip_evaluation)
  250.                 error ("division by zero in #if");
  251.                   $3.value = 1;
  252.                 }
  253.               $$.unsignedp = $1.unsignedp || $3.unsignedp;
  254.               if ($$.unsignedp)
  255.                 $$.value = (unsigned long) $1.value % $3.value;
  256.               else
  257.                 $$.value = $1.value % $3.value; }
  258.     |    exp '+' exp
  259.             { $$.value = $1.value + $3.value;
  260.               $$.unsignedp = $1.unsignedp || $3.unsignedp;
  261.               if (! $$.unsignedp
  262.                   && ! possible_sum_sign ($1.value, $3.value,
  263.                               $$.value))
  264.                 integer_overflow (); }
  265.     |    exp '-' exp
  266.             { $$.value = $1.value - $3.value;
  267.               $$.unsignedp = $1.unsignedp || $3.unsignedp;
  268.               if (! $$.unsignedp
  269.                   && ! possible_sum_sign ($$.value, $3.value,
  270.                               $1.value))
  271.                 integer_overflow (); }
  272.     |    exp LSH exp
  273.             { $$.unsignedp = $1.unsignedp;
  274.               if ($3.value < 0 && ! $3.unsignedp)
  275.                 $$.value = right_shift (&$1, -$3.value);
  276.               else
  277.                 $$.value = left_shift (&$1, $3.value); }
  278.     |    exp RSH exp
  279.             { $$.unsignedp = $1.unsignedp;
  280.               if ($3.value < 0 && ! $3.unsignedp)
  281.                 $$.value = left_shift (&$1, -$3.value);
  282.               else
  283.                 $$.value = right_shift (&$1, $3.value); }
  284.     |    exp EQUAL exp
  285.             { $$.value = ($1.value == $3.value);
  286.               $$.unsignedp = 0; }
  287.     |    exp NOTEQUAL exp
  288.             { $$.value = ($1.value != $3.value);
  289.               $$.unsignedp = 0; }
  290.     |    exp LEQ exp
  291.             { $$.unsignedp = 0;
  292.               if ($1.unsignedp || $3.unsignedp)
  293.                 $$.value = (unsigned long) $1.value <= $3.value;
  294.               else
  295.                 $$.value = $1.value <= $3.value; }
  296.     |    exp GEQ exp
  297.             { $$.unsignedp = 0;
  298.               if ($1.unsignedp || $3.unsignedp)
  299.                 $$.value = (unsigned long) $1.value >= $3.value;
  300.               else
  301.                 $$.value = $1.value >= $3.value; }
  302.     |    exp '<' exp
  303.             { $$.unsignedp = 0;
  304.               if ($1.unsignedp || $3.unsignedp)
  305.                 $$.value = (unsigned long) $1.value < $3.value;
  306.               else
  307.                 $$.value = $1.value < $3.value; }
  308.     |    exp '>' exp
  309.             { $$.unsignedp = 0;
  310.               if ($1.unsignedp || $3.unsignedp)
  311.                 $$.value = (unsigned long) $1.value > $3.value;
  312.               else
  313.                 $$.value = $1.value > $3.value; }
  314.     |    exp '&' exp
  315.             { $$.value = $1.value & $3.value;
  316.               $$.unsignedp = $1.unsignedp || $3.unsignedp; }
  317.     |    exp '^' exp
  318.             { $$.value = $1.value ^ $3.value;
  319.               $$.unsignedp = $1.unsignedp || $3.unsignedp; }
  320.     |    exp '|' exp
  321.             { $$.value = $1.valu